package demo.sort;

import java.util.*;

/**
 * @author lingyun.wang on @date 2021/12/27
 */
public class SortEntry extends AbstractMap.SimpleEntry {
    private static List<SortBO> sortBOS;

    public SortEntry(Object key, Object value,List<SortBO> sortBOS) {
        super(key, value);
        setSortBOS(sortBOS);
    }

    public SortEntry(Map.Entry entry,List<SortBO> sortBOS) {
        super(entry);
        setSortBOS(sortBOS);
    }

    private static void setSortBOS(List<SortBO> sortBOS) {
        SortEntry.sortBOS = sortBOS;
    }

    public static int compareTo(Map<String, Object> o1,Map<String, Object> o2){
        int result = -1;
        for (SortBO sortBO : sortBOS) {
            System.out.println("compareTo"+o1.get(sortBO.getField())+","+o2.get(sortBO.getField()));
            if(o1.get(sortBO.getField()) instanceof Integer){
                result = ((Integer)o1.get(sortBO.getField())).compareTo((Integer)o2.get(sortBO.getField()));
            }else {
                result = o1.get(sortBO.getField()).toString().compareTo(o2.get(sortBO.getField()).toString());

            }
            if(1 == result){
                return result;
            }
        }
        return result;
    }

    public AbstractMap.SimpleEntry toEntry() {
        return this;
    }
}
